package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

	@Autowired
	private OrderInfoService orderInfoService;

	@Operation(summary = "获取用户订单")
	@GetMapping("findUserPage/{page}/{limit}")
	public Result findUserPage(@PathVariable Long pageNo,
							   @PathVariable Long PageSize,
							   OrderInfo orderInfo) {
		Long userId = AuthContextHolder.getUserId();
		 Page<OrderInfo> page = new Page<>(pageNo, PageSize);
		IPage<OrderInfo> iPage= orderInfoService.findUserPage(page,orderInfo.getOrderStatus(),userId);
		return Result.ok(iPage);
	}


	/**
	 * 根据订单编号查询订单信息
	 * @param orderNo
	 * @return
	 */

	@Operation(summary = "根据订单编号查看订单")
	@GetMapping("/getOrderInfo/{orderNo}")
	public Result getOrderInfoByOrderNo(@PathVariable String orderNo){
		//	调用服务层方法
		OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderNo(orderNo);
		//	返回数据
		return Result.ok(orderInfo);
	}
	/**
	 * 确认订单
	 * @param tradeVo
	 * @return
	 */
	@Operation(summary = "确认订单")
	@PostMapping("trade")
	public Result trade(@RequestBody TradeVo tradeVo) {
		Long userId = AuthContextHolder.getUserId();
		OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo, userId);
		return Result.ok(orderInfoVo);
	}
	/**
	 * 提交订单
	 * @param orderInfoVo
	 * @return
	 */
	@Operation(summary = "提交订单")
	@PostMapping("submitOrder")
	public Result submitOrder(@RequestBody @Validated OrderInfoVo orderInfoVo) {
		Long userId = AuthContextHolder.getUserId();

		String orderNo = orderInfoService.submitOrder(orderInfoVo, userId);
		Map<String, Object> result =new HashMap<>();
		result.put("orderNo", orderNo);
		return Result.ok(result);
	}

}

